<template>
	<el-dialog :title="titleMap[mode]" v-model="visible"  customClass="customWidth"  destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
				<el-form-item label="图表名称" prop="title">
					<el-input v-model="form.title" placeholder="图表名称" maxlength="48" clearable></el-input>
				</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="排序号" prop="sortNo">
						<el-input-number v-model="form.sortNo" :min="1" style="width: 100%;"></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="主标识" prop="itemKey">
						<el-input v-model="form.itemKey" placeholder="主标识" maxlength="48" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="图表分类" prop="itemId">
						<el-cascader v-model="form.itemId" :options="groups" :props="groupsProps" :show-all-levels="false" clearable style="width: 100%;"></el-cascader>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="图表标识" prop="chartKey">
						<el-input v-model="form.chartKey" placeholder="图表标识" maxlength="48" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="框架标识" prop="category">
						<el-input v-model="form.category" placeholder="框架标识" maxlength="48" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="类别名称" prop="categoryName">
						<el-input v-model="form.categoryName" placeholder="类别名称" maxlength="48" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="图表框架" prop="chartFrame">
						<el-input v-model="form.chartFrame" placeholder="图表框架" maxlength="48" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="程序包" prop="packageType">
						<el-input v-model="form.packageType" placeholder="程序包" maxlength="48" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="示意图" prop="image">
						<el-input v-model="form.image" placeholder="示意图" maxlength="48" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="是否启用" prop="status">
						<el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="默认值" prop="defaultValue">
				<el-input type="textarea" :rows="2" placeholder="默认值" v-model="form.defaultValue"></el-input>
			</el-form-item>
			<el-form-item label="备注说明">
				<el-input type="textarea" :rows="2" maxlength="180" placeholder="备注说明" v-model="form.remark"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	components: {
	},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增图表',
				edit: '编辑图表'
			},
			groups: [],
			groupsProps: {
				label:"name",
				value: "itemId",
				emitPath: false,
				checkStrictly: true
			},
			form: {
				recordId: "",
				title: "",
				sortNo:0,
				itemKey:"",
				chartKey: "",
				itemId: "",
				category: "",
				categoryName: "",
				packageType: "",
				chartFrame: "",
				image: "",
				defaultValue: "",
				remark:"",
				status: "1"
			},
			rules: {
				title: [
					{required: true, message: '图表名称不能为空'}
				],
				itemKey: [
					{required: true, message: 'ITEM_KEY不能为空'}
				],
				chartKey: [
					{required: true, message: 'CHART_KEY不能为空'}
				],
				itemId:[
					{required: true, message: '图表分类不能为空'}
				],
				category: [
					{required: true, message: 'CATEGORY不能为空'}
				],
				categoryName: [
					{required: true, message: 'CATEGORY_NAME不能为空'}
				],
				packageType: [
					{required: true, message: 'PACKAGE_TYPE不能为空'}
				],
				chartFrame: [
					{required: true, message: 'CHART_FRAME不能为空'}
				],
				defaultValue: [
					{required: true, message: '默认值不能为空'}
				],
				image: [
					{required: true, message: '示意图不能为空'}
				],
				sortNo: [
					{required: true, message: '请输入排序', trigger: 'change'}
				],
			},
			visible: false,
			isSaveing: false,
		}
	},
	mounted() {
		this.getBigDataItemTree();
	},
	methods: {
		async getBigDataItemTree(){
			var res = await this.$API.bigdata.bigDataItem.getBigDataItemTree.get();
			this.groups = res.data;
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					var res;
					if(this.mode=="add")
					{
						this.isSaveing = true;
						res = await this.$API.bigdata.bigDataChart.insertBigDataChart.post(this.form);
						this.isSaveing = false;
					}else if(this.mode=="edit")
					{
						this.isSaveing = true;
						res = await this.$API.bigdata.bigDataChart.updateBigDataChart.post(this.form);
						this.isSaveing = false;
					}
					if (res.code == 200) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.recordId = data.recordId
			this.form.title = data.title
			this.form.sortNo = data.sortNo
			this.form.itemKey = data.itemKey
			this.form.chartKey = data.chartKey
			this.form.itemId = data.itemId
			this.form.category = data.category
			this.form.categoryName = data.categoryName
			this.form.packageType = data.packageType
			this.form.chartFrame = data.chartFrame
			this.form.image = data.image
			this.form.defaultValue = data.defaultValue
			this.form.remark = data.remark
			this.form.status = data.status
		}
	}
}
</script>

<style>
.customWidth {
	width: 50%;
}
</style>
